package com.yanqu.road.dao.impl.activity.xiuxian;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.yanqu.road.dao.db.DbNameStrategy;
import com.yanqu.road.dao.db.DbParameter;
import com.yanqu.road.dao.impl.TempDataObjectDao;
import com.yanqu.road.entity.activity.xiuxian.data.*;

import java.sql.CallableStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Types;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossXiuXianPvpUserDataDaoImpl extends TempDataObjectDao<CrossXiuXianPvpUserData> {

    public static String getCrossXiuXianPvpUserDataSql = "select * from `t_c_xiuxian_pvp_user_data` where `activityId`=?";

    public Map<Long, CrossXiuXianPvpUserData> getCrossXiuXianPvpUserDataMap(int activityId) {
        Map<Long, CrossXiuXianPvpUserData> map = new ConcurrentHashMap<>();
        ResultSet rs = null;
        CallableStatement call = null;
        try {
            getDb().setConnName(DbNameStrategy.getCross2Db());
            call = getDb().getConnection().prepareCall(getCrossXiuXianPvpUserDataSql);
            int i = 1;
            call.setInt(i++, activityId);
            rs = call.executeQuery();
            while (rs.next()) {
                CrossXiuXianPvpUserData parse = parse(rs);
                map.put(parse.getUserId(), parse);
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        } finally {
            getDb().closeConnection(call, rs);
        }
        return map;
    }

    public static String insertCrossXiuXianPvpUserDataSql = "insert into `t_c_xiuxian_pvp_user_data`(activityId,userId,pveProperty,pvpProperty,campId,placeId,position,transferTime,attackTime,attackedTime,reviveTime,updatePropertyTime,totalUseEnergy,updateEnergyTime,reviveCnt,attackScore,seizeScore,updateScoreTime,battleRecord,battleRecordMap,lastPingPlaceTime,mountData,pvpAble) values (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)";

    @Override
    protected boolean addData(CrossXiuXianPvpUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPveProperty())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPvpProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlaceId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPosition())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTransferTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackedTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReviveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdatePropertyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalUseEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateEnergyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReviveCnt()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSeizeScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBattleRecord())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBattleRecordMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastPingPlaceTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMountData())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isPvpAble()));
        if (getDb().executeNoneQuery(insertCrossXiuXianPvpUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    public static String updateCrossXiuXianPvpUserDataSql = "update `t_c_xiuxian_pvp_user_data` set pveProperty=?,pvpProperty=?,campId=?,placeId=?,position=?,transferTime=?,attackTime=?,attackedTime=?,reviveTime=?,updatePropertyTime=?,totalUseEnergy=?,updateEnergyTime=?,reviveCnt=?,attackScore=?,seizeScore=?,updateScoreTime=?,battleRecord=?,battleRecordMap=?,lastPingPlaceTime=?,mountData=?,pvpAble=? where activityId=? and userId=?";

    @Override
    protected boolean updateData(CrossXiuXianPvpUserData data) {
        data.setBeginWriteTime();
        getDb().setConnName(DbNameStrategy.getCross2Db());
        int i = 1;
        Map<Integer, DbParameter> para = new HashMap<>();
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPveProperty())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPvpProperty())));
        para.put(i++, new DbParameter(Types.INTEGER, data.getCampId()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getPlaceId()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getPosition())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getTransferTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackedTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getReviveTime()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdatePropertyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getTotalUseEnergy()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateEnergyTime()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getReviveCnt()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getAttackScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getSeizeScore()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUpdateScoreTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBattleRecord())));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getBattleRecordMap())));
        para.put(i++, new DbParameter(Types.BIGINT, data.getLastPingPlaceTime()));
        para.put(i++, new DbParameter(Types.VARCHAR, JSON.toJSONString(data.getMountData())));
        para.put(i++, new DbParameter(Types.BOOLEAN, data.isPvpAble()));
        para.put(i++, new DbParameter(Types.INTEGER, data.getActivityId()));
        para.put(i++, new DbParameter(Types.BIGINT, data.getUserId()));
        if (getDb().executeNoneQuery(updateCrossXiuXianPvpUserDataSql, para) > 0) {
            return true;
        }
        return false;
    }

    @Override
    protected CrossXiuXianPvpUserData parseData(ResultSet rs) throws SQLException {
        CrossXiuXianPvpUserData data = new CrossXiuXianPvpUserData();
        data.setActivityId(rs.getInt("activityId"));
        data.setUserId(rs.getLong("userId"));
        data.setPveProperty(JSON.parseObject(rs.getString("pveProperty"), new TypeReference<XiuXianPveUserProperty>() {
        }));
        data.setPvpProperty(JSON.parseObject(rs.getString("pvpProperty"), new TypeReference<XiuXianPvpUserProperty>() {
        }));
        data.setCampId(rs.getInt("campId"));
        data.setPlaceId(rs.getInt("placeId"));
        data.setPosition(JSON.parseObject(rs.getString("position"), new TypeReference<CrossXiuXianPvpPosition>() {
        }));
        data.setTransferTime(rs.getLong("transferTime"));
        data.setAttackTime(rs.getLong("attackTime"));
        data.setAttackedTime(rs.getLong("attackedTime"));
        data.setReviveTime(rs.getLong("reviveTime"));
        data.setUpdatePropertyTime(rs.getLong("updatePropertyTime"));
        data.setTotalUseEnergy(rs.getInt("totalUseEnergy"));
        data.setUpdateEnergyTime(rs.getLong("updateEnergyTime"));
        data.setReviveCnt(rs.getInt("reviveCnt"));
        data.setAttackScore(rs.getLong("attackScore"));
        data.setSeizeScore(rs.getLong("seizeScore"));
        data.setUpdateScoreTime(rs.getLong("updateScoreTime"));
        data.setBattleRecord(JSON.parseObject(rs.getString("battleRecord"), new TypeReference<CrossXiuXianPvpBattleRecord>() {
        }));
        data.setBattleRecordMap(JSON.parseObject(rs.getString("battleRecordMap"), new TypeReference<ConcurrentHashMap<Integer, CrossXiuXianPvpBattleRecord>>() {
        }));
        data.setLastPingPlaceTime(rs.getLong("lastPingPlaceTime"));
        data.setMountData(JSON.parseObject(rs.getString("mountData"), new TypeReference<CrossXiuXianUserMountData>() {}));
        data.setPvpAble(rs.getBoolean("pvpAble"));
        return data;
    }
}
